
import database.Restaurant;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.jdesktop.swingx.JXImagePanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * insertpanel.java
 *
 * Created on 29 Ιαν 2012, 5:10:50 μμ
 */
/**
 *
 * @author karolos
 */
public class insertpanel extends javax.swing.JPanel {

    DBManager db = mainframe.getDB();  //db poy dimiourgi8ike stin main
    private Image originalimage; //eikona estiatoriou
    private String imagepath;   //onoma arxeioy fotografias
    /** Creates new form insertpanel */
    public insertpanel() {
        initComponents();
        initTable(); //symplirosi pedion combobox kai jlist apo basi dedomenon

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    //dhmiourgia restaurant 
    public Restaurant createRestaurant() {  //dhmiourgia estiatorioy
        Restaurant r1 = new Restaurant();
        r1.setPhoto(imagepath);
        r1.setName(restname.getText());
        r1.setAddress(restaddress.getText());
        r1.setPhonenumber(restphonenumber.getText());
        r1.setInfo(restinfo.getText());
        r1.setDescription(restdescription.getText());

        return r1;
    }
    //arxikopoiisi timon

    private void initTable() {
        jButton4.setVisible(false);//8a ginei enable kata tin tropopoihsh estiatorioy.
        ArrayList<Integer> al = db.showAreas();//emfanisi perioxon se combobox
        for (int i = 0; i < al.size(); i++) {
            restarea.addItem(al.get(i));
        }
        ArrayList<String> al2 = db.showPriceRange();//emfanisi eurous timon se combobox
        for (int i = 0; i < al2.size(); i++) {
            restpricerange.addItem(al2.get(i));
        }

        ArrayList<String> al3 = db.showCategory();//emfanisi katigorion se jlist
        DefaultListModel info = new DefaultListModel();

        for (int i = 0; i < al3.size(); i++) {
            info.addElement(al3.get(i));
        }
        restcategory.setModel(info);
        restname.setText("");
        restaddress.setText("");
        restphonenumber.setText("");
        restdescription.setText("");
        restinfo.setText("");
        jXImagePanel1.setImage(null);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        restaddress = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        restname = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        restarea = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        restcategory = new javax.swing.JList();
        restphonenumber = new javax.swing.JTextField();
        restinfo = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        restdescription = new javax.swing.JTextArea();
        restpricerange = new javax.swing.JComboBox();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jXImagePanel1 = new org.jdesktop.swingx.JXImagePanel();

        setBackground(new java.awt.Color(255, 255, 255));
        setLayout(new java.awt.BorderLayout());

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(70, 35));

        jButton4.setText("Ενημέρωση");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton4);

        jButton1.setText("Καταχώρηση");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        add(jPanel1, java.awt.BorderLayout.SOUTH);

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setPreferredSize(new java.awt.Dimension(70, 35));

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Εισαγωγή Εστιατορίου");
        jPanel2.add(jLabel1);

        add(jPanel2, java.awt.BorderLayout.NORTH);

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel2.setText("Όνομα εστιατορίου:");
        jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 20, -1, -1));
        jPanel3.add(restaddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 50, 240, -1));

        jLabel3.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel3.setText("Διεύθυνση:");
        jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 50, -1, -1));
        jPanel3.add(restname, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 20, 240, -1));

        jLabel6.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel6.setText("Κατηγορία:");
        jPanel3.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 160, -1, -1));

        jLabel4.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel4.setText("Περιοχή:");
        jPanel3.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 80, -1, -1));

        jLabel5.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel5.setText("Τηλέφωνο:");
        jPanel3.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 110, -1, -1));

        jLabel7.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel7.setText("Πληροφορίες:");
        jPanel3.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 220, -1, -1));

        jLabel8.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel8.setText("Περιγραφή:");
        jPanel3.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(80, 270, -1, -1));

        jLabel9.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel9.setText("Εύρος τιμών:");
        jPanel3.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 330, -1, -1));

        jLabel10.setFont(new java.awt.Font("Arial", 0, 11));
        jLabel10.setText("Φωτογραφία:");
        jPanel3.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 380, -1, -1));

        restarea.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onClick(evt);
            }
        });
        jPanel3.add(restarea, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 80, 240, -1));

        restcategory.setToolTipText("");
        jScrollPane1.setViewportView(restcategory);

        jPanel3.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 145, 241, 60));
        jPanel3.add(restphonenumber, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 110, 240, -1));
        jPanel3.add(restinfo, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 220, 240, -1));

        restdescription.setColumns(20);
        restdescription.setRows(5);
        jScrollPane2.setViewportView(restdescription);

        jPanel3.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 250, 240, 70));

        jPanel3.add(restpricerange, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 330, 240, -1));

        jButton2.setText("Προσθήκη");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(320, 370, -1, 40));

        jButton3.setText("Αφαίρεση");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        jPanel3.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(430, 370, -1, 40));

        jXImagePanel1.setBackground(new java.awt.Color(255, 255, 255));
        jXImagePanel1.setBorder(new javax.swing.border.MatteBorder(null));
        jXImagePanel1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jXImagePanel1MouseClicked(evt);
            }
        });
        jPanel3.add(jXImagePanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 360, 80, 60));

        add(jPanel3, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    //energopoihsh filtroy kai eisagogi eikonas apo filechooser se epi8ymites diastaseis
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        JFileChooser fc = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Image files", "jpg",
                "jpeg", "bmp", "tiff", "tif", "gif");
        fc.setFileFilter(filter);
        int returnVal = fc.showOpenDialog(null);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                File file = fc.getSelectedFile();
                imagepath=file.getName();
                
                originalimage = ImageIO.read(file);
                Image scaledimage = originalimage.getScaledInstance(80, 60, WIDTH);
                jXImagePanel1.setImage(scaledimage);
            } catch (IOException ex) {
                Logger.getLogger(insertpanel.class.getName()).log(Level.SEVERE, null, ex);
            }


        } else;
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        jXImagePanel1.setImage(null); //afairesi fotografias
        this.setImagepath(null);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jXImagePanel1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jXImagePanel1MouseClicked

        Image imagenew = originalimage.getScaledInstance(200, 200, WIDTH);
        photoshow ps = new photoshow(); //dhmioyrgia antikeimenoy(para8yro fotografias)
        if ((ps.getCount() >= 2)) //elegxos an kapoia foto einai hdh anoixth
        {
            ;

        } else if ((jXImagePanel1.getImage() == null)) { //elegxos an yparxei fotografia
            ps.setCount(0);
        } else {                                         //probolh fotografias sto imagepanel
            ps.setVisible(true);
            ps.setDefaultCloseOperation(WIDTH);
            ps.getImagePanel().setImage(imagenew);
        }

    }//GEN-LAST:event_jXImagePanel1MouseClicked
    //dhmiourgia restaurant kai apo8ikeysi sti basi
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Restaurant r = createRestaurant();
        int cid = db.getcatid((String) restcategory.getSelectedValue());
        int pid = db.getpriceid((String) restpricerange.getSelectedItem());
        int aid = db.getareaid((String) restarea.getSelectedItem());
        
        db.storeRestaurant(r, cid, pid, aid);
        initTable();//arxikopoihsi pedion (jcombobox,jlist)



    }//GEN-LAST:event_jButton1ActionPerformed

    private void onClick(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onClick
    }//GEN-LAST:event_onClick
//enimerosi estiatoriou
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed

        Restaurant r2 = createRestaurant();
        int cid2 = db.getcatid((String) restcategory.getSelectedValue());
        int pid2 = db.getpriceid((String) restpricerange.getSelectedItem());
        int aid2 = db.getareaid((String) restarea.getSelectedItem());
        Restaurant r1 = editpanel.getrestaurant();
        int cid1 = r1.getCid();
        int pid1 = r1.getPid();
        int aid1 = r1.getAid();
      
        db.updaterestaurant(r2, r1, cid2, pid2, aid2, cid1, pid1, aid1);

    }//GEN-LAST:event_jButton4ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private org.jdesktop.swingx.JXImagePanel jXImagePanel1;
    private javax.swing.JTextField restaddress;
    private javax.swing.JComboBox restarea;
    private javax.swing.JList restcategory;
    private javax.swing.JTextArea restdescription;
    private javax.swing.JTextField restinfo;
    private javax.swing.JTextField restname;
    private javax.swing.JTextField restphonenumber;
    private javax.swing.JComboBox restpricerange;
    // End of variables declaration//GEN-END:variables

    public JTextField getRestaddress() {
        return restaddress;
    }

    public JComboBox getRestarea() {
        return restarea;
    }

    public JList getRestcategory() {
        return restcategory;
    }

    public JTextArea getRestdescription() {
        return restdescription;
    }

    public JTextField getRestinfo() {
        return restinfo;
    }

    public JTextField getRestname() {
        return restname;
    }

    public JTextField getRestphonenumber() {
        return restphonenumber;
    }

    public JComboBox getRestpricerange() {
        return restpricerange;
    }
    // End of variables declaration

// methodoi poy kaloyn alles klaseis fia show/hide koympion enhmerosis kai kataxorisis
    public JButton getbutton1() {

        return jButton1;
    }

    public JButton getbutton4() {

        return jButton4;
    }

    public JXImagePanel getjXImagePanel1() {
        return jXImagePanel1;
    }

    public void setjXImagePanel1(JXImagePanel jXImagePanel1) {
        this.jXImagePanel1 = jXImagePanel1;
    }

    public String getImagepath() {
        return imagepath;
    }

    public void setImagepath(String imagepath) {
        this.imagepath = imagepath;
    }

    public Image getOriginalimage() {
        return originalimage;
    }

    public void setOriginalimage(Image originalimage) {
        this.originalimage = originalimage;
    }


}
